//index.js
//获取应用实例
var WxSearch = require('../../wxSearchView/wxSearchView.js');
var netWork = require('../../assets/js/network.js')
const app = getApp()
Page({
  data: {
    reque:true,
    text: 'hello me',    
    imgUrls: [],
    framePic:'../../images/bofang.png',
    proList: [],
    channels: [],
    pageNumber:1,
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    bg: '#C79C77'
  },
  tuijian: function (e) {
    this.setData({
      reque: true
    })
  },
  img1: function (e) {
    wx.navigateTo({
      url: `../../pages/content/content?id=${e.currentTarget.dataset.id}`
    })
  },
  pindao: function (e) {
    this.setData({
      reque:false
    })
  },
  tz: function (e) {
    wx.navigateTo({
      url: `../../pages/searResponse/response?typeid=${e.currentTarget.dataset.typeid}`,
    })
  },
  onLoad: function (options) {
    this.authorization();
    this.getBanner();
    this.bannerLink();
    this.channelsList();
  },
  onReady:function(){
   
  },
  onPullDownRefresh: function () {
    this.bannerLink()
  },
  channelsList:function(){
    wx.request({
      url: `${netWork.api}wztype/list?pageSize=20&currentPage=1&sidx=&order=asc`,
      success:res=>{
        if(!res.data.code)
          this.setData({
            channels:res.data.page.list
          })
      }
    })
  },
  authorization:function(){
    let This = this;
    wx.getUserInfo({
      withCredentials: true,
      success: function (res) {
        //此处为获取微信信息后的业务方法
             wx.request({
              url: `${netWork.api}wzuser/login`,
              data: {
                code: wx.getStorageSync('userCode'),
                encryptedData: res.encryptedData,
                iv: res.iv
              },
              header: { "Content-Type": "application/x-www-form-urlencoded"},
              method: 'POST',
              //服务端的回掉  
              success: function (result) {
                  wx.setStorageSync('token', result.data.token)
              }
            })

      },
      fail: function (rs) {
        //获取用户信息失败后。请跳转授权页面
        wx.showModal({
          title: '提示',
          content: '需要使用您的授权获取用户名称等信息，请点击确定跳转到授权页面进行授权。',
          success: function (res) {
            if (res.confirm) {
              wx.navigateTo({
                url: '../../pages/authorization/authorization',
              })
            } else if (res.cancel) {
              This.authorization()
            }
          }
        })
      }
    })
  },
  getBanner: function() {
   wx.request({
     url: `${netWork.api}wzbanner/list?pageSize=6&currentPage=0&sidx=&order=asc`,
     success:res=>{
       this.setData({
         imgUrls: res.data.page.list
       })
     }
   })
  },
  bannerLink:function(){
    var This = this;
    wx.request({
      url: `${netWork.api}wzshortfilm/list?pageSize=10&currentPage=${This.data.pageNumber}&sidx=sysCreateTime&order=desc&typePid=&shortfilmName=`,
      success: res => {
        if (!res.data.code && res.data.page.list.length) 
          This.setData({
            proList:This.data.proList.concat(res.data.page.list),
            pageNumber: This.data.pageNumbe+1
          })
        else
          wx.showToast({
            title: '到底了！',
            icon: 'none',
            mask:false
          })
      }
    })
  },
   onReachBottom: function () {
     this.bannerLink();
  }
})
